package com.hpeng.daily.action;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.service.StaMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardExwarehouseAction")
@Scope("prototype")
public class AssetCardExwarehouseAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page;
	public String pageLink;
	private Pagination<AssetCard> pagination;
	private List<Lookup> ls;// 用于页面上显示资产的状态的list
	
	//用于根据权限生成功能按钮
	private String FN;
	private String accessRight;
	@Resource
	private Function function;
	
	//资产卡片的业务层
	@Resource
	private AssetCardManager assetCardManager;
	@Resource
	private LoginUserMgr loginUser;
	@Resource
	private StaMgr staMgr;
	private AssetCard assetCard;	//资产卡片
	private String acid;	//物品编号
	private String catid;
	private String msg;
	private String idArray; // 获取被选择的Id编号数组 从页面上传到后台的数组
	private String acuser;//不属于公共的资产的使用人
	//条件查询
	private String conditionValue;
	private String condition;
	private String acstatusValue;
	
	/*********************资产出库与接收管理*******************************/	
	public String searchExwarehouse(){
		//1.状态为: 入库-物资部领导已确认的资产进行出库管理
		//2.状态为: 出库-申请部门已接受时, 可以对资产的归属类别做选择.归个人还是公共
		String order = " ";
		String[] hql = { " from AssetCard where acstatus='"
				+ 203 + "' or acstatus like '" + 3 + "%' "};
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("searchExwarehouse.action?FN=" + FN);
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return "success";
	}
	//点击树上的资产类别得到对应该类别下资产状态为出库以及入库---物资部领导已审核的资产情况
	public String queryExwarehouseByCategory(){
		String[] hql = { " from AssetCard where acstatus='203' and catid like '"
				+ catid + "%' or catid like '" + catid + "%' and acstatus like '3%' " };
		String order = " ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("queryExwarehouseByCategory.action?FN=" + FN
				+ "&catid=" + catid);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		this.accessRight = function.getAccessRight(session(), FN);
		return "success";
	}
	
	//出库-物资部经办已确认
	public String updateExwarehouseGoodsManagerConfirm(){
		try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("203")){
			     function.updStatus("3", "40", stringArray);
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("3", "40"));
			  }else{
				msg=getText("物资部经办确认的只能是状态处于入库物资部领导已审核的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("确认失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("确认成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return searchExwarehouse(); 
	}
	//出库--接收部门已确认
	public String updateExwarehouseAcceptManagerConfirm(){
		try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("340")){
			     function.updStatus("3", "41", stringArray);
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("3", "41"));
			  }else{
				msg=getText("接收部经办确认的只能是状态处于出库物资部经办已确认的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("确认失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("确认成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return searchExwarehouse();
	}
    //接收部门物资待分配
	public String updateExwarehouseAcceptManagerAllot(){
		try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("341")){
			     function.updStatus("3", "42", stringArray);
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("3", "42"));
			  }else{
				msg=getText("接收部门物资待分配的只能是状态处于出库接收部经办确认的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("确认失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("确认成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return searchExwarehouse();
	}
	//当资产不属于公用时  将资产进行分配给个人使用
	public String updateExwarehouseAllotPersonal(){
		try{
			String[] stringArray = idArray.split(",");
		for (int i = 0; i < stringArray.length; i++) {
			assetCard = assetCardManager.findAssetCard(stringArray[i]);
			if(assetCard.getAcstatus().equals("342")){//此时资产状态变为在用
				 assetCard.setIspublic("N");//此时  资产公共状态变为N 为不公用
				 assetCard.setAcuser(acuser);//设置使用人
				 assetCard.setDepname(staMgr.getStaffByName(acuser).get(0).getDepartment().getDepname());
				 assetCard.setAcstatus("400");
			     assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("4", "00"));
			   if(assetCard.getAcstatus().equals("Y")){
				   assetCard.setDepname(assetCard.getReportdepartment());
				   assetCardManager.updateAssetCard(assetCard,"E",loginUser.getUserId(request()),null,function.getStatusDesc("4", "00"));
				   msg = getText("资产属于公用资产，已经保存");
			   } 
			}else{
				msg=getText("分配的只能是状态处于出库接收部门物资待分配的资产");
			  }
		   }
		} catch (Exception rx) {
			msg = getText("分配资产失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("分配资产成功");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return searchExwarehouse();
	}

	// 按照条件进行查询
	public String queryExwarehouseAssetCard() {
		try {
			if(conditionValue!=null){
			       conditionValue = new String(conditionValue.getBytes("ISO8859-1"),
					"UTF-8");
			       }
			String append = "";
			if ("zclb".equals(condition)) {
				append = " and category.catname like ('%"+conditionValue+"%') ";
			} else if ("zczt".equals(condition)) {
				append = " and acstatus like ('%"+acstatusValue+"%') ";
			} 
			String[] hql = {
					" from AssetCard where acstatus like '"
				+ 203 + "' or acstatus like '" + 3 + "%' ",
					append };
			String order = " order by acid asc";
			setPagination(assetCardManager.getPagination(page, hql, order));
			pageLink = pagination
					.getPageLink("queryExwarehouseAssetCard.action?condition=" + condition
							+ "&conditionValue=" + conditionValue+"&acstatusValue=" + acstatusValue);
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		}
		return "success";
	}
	
	
	
	
	/****************set    get 方法*************************/
	public Pagination<AssetCard> getPagination() {
		return pagination;
	}
	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public void setAssetCardManager(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}
	public void setAssetCard(AssetCard assetCard) {
		this.assetCard = assetCard;
	}
	public void setAcid(String acid) {
		this.acid = acid;
	}
	public void setFN(String fN) {
		FN = fN;
	}
	
	public HttpServletRequest request(){
		return ServletActionContext.getRequest();
	}
	public Map<String,Object> session(){
		return ActionContext.getContext().getSession();
	}

	public String getAccessRight() {
		return accessRight;
	}
	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}

	public List<Lookup> getLs() {
		return ls;
	}
	public void setLs(List<Lookup> ls) {
		this.ls = ls;
	}

	public String getCatid() {
		return catid;
	}
	public void setCatid(String catid) {
		this.catid = catid;
	}
	public String getIdArray() {
		return idArray;
	}
	public void setIdArray(String idArray) {
		this.idArray = idArray;
	}

	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getAcuser() {
		return acuser;
	}
	public void setAcuser(String acuser) {
		this.acuser = acuser;
	}

	public String getConditionValue() {
		return conditionValue;
	}
	public void setConditionValue(String conditionValue) {
		this.conditionValue = conditionValue;
	}
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	public String getAcstatusValue() {
		return acstatusValue;
	}
	public void setAcstatusValue(String acstatusValue) {
		this.acstatusValue = acstatusValue;
	}
	public String getFN() {
		return FN;
	}
	public String getAcid() {
		return acid;
	}
}
